#! /usr/bin/env python
# -*- coding: utf-8 -*-

import qqip
import sys

curdir=sys.path[0]

f=open("%s/sample_ips.log"%curdir)
g=f.readlines()
f.close()

a=[]
for i in g:
	k=i.split()	
	a.append(k[0])

a=list(set(a))

ip_locater = qqip.IpLocater(r'%s/qqwry.db'%curdir)

ips=[]

print 'Reading IPs from server log'
for i in a:
	address = ip_locater.getIpAddr(qqip.string2ip(i))
	host=address.decode('gb18030').encode('utf8')
	host=host.split('/')
	host[0]=host[0].strip()
	host[0]=host[0].strip('CZ88.NET')
	host[1]=host[1].strip()
	host[1]=host[1].strip('CZ88.NET')
	if  ('市' in host[0] or '省' in host[0] or  '中国' in host[0] or '大学' in host[0]) and not ('雅虎' in host[0] or '雅虎' in host[1] or  '蜘蛛' in host[1] or '百度' in host[1] or '腾讯' in host[1]):
		ips.append([i,host[0], host[1]])

ips.sort()
f=open('%s/ips.txt'%curdir, 'w')
for i in ips:
	f.write( '%16s | %s | %s\n'% (i[0], i[1], i[2]))

f.close()

